/*
 * Filename:    WarnResultController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月12日
 */
package com.paic.mhis.hcpms.warning.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.mhis.hcpms.warning.biz.service.HcpmsWarnService;
import com.paic.mhis.hcpms.warning.biz.service.WarnResultDetailService;
import com.paic.mhis.hcpms.warning.biz.service.WarnResultService;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarningDTO;
import com.paic.mhis.hcpms.warning.dto.WarnMonitorDTO;
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/warnResult/")
public class WarnResultController {
	@BizServicesProxy
	private WarnResultService warnResultService;
	@BizServicesProxy
	private WarnResultDetailService warnResultDetailService;
	@BizServicesProxy
	private WarningWorkFlowService warningWorkFlowService;
	@BizServicesProxy
	private HcpmsWarnService hcpmsWarnService;

	@BizServicesProxy(beanName = "integrityMedicalService")
	private IntegrityMedicalService integrityMedicalService;

	@RequestMapping(value = "warnResult.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> warnResult(HttpServletRequest request) throws BusinessServiceException,
			BusinessException, UnsupportedEncodingException, IOException {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String str = HttpProcessUtil.readRequestBody(request);
		JSONObject jarray = JSON.parseObject(str);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			integrityMedicalService.addIntegrityPointTemps(jarray, user.getUsername());// 临时扣分
			warnResultDetailService.addWarnResultDetail(jarray, paramMap);
			int result = warnResultService.addWarnResult(jarray, paramMap);
			if (result > 0) {
				// 启动
				String warnNo = jarray.get("warnNo").toString();
				String opinion = jarray.get("opinion").toString();
				HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
				Map<String, String> condition = new HashMap<String, String>();
				condition.put("SITUATION", opinion);
				List<Authority> authorityList = user.getAuthorities();
				List<String> role = new ArrayList<String>();
				for (Authority authority : authorityList) {
					role.add(authority.getAuthority());
				}
				wfvo.setRole(role);
				wfvo.setTaskNo(warnNo);
				wfvo.setActCurrent(user.getUsername());
				wfvo.setParamValue(condition);
				String actCurrent = warningWorkFlowService.processWorkFlow(wfvo);
				// 更新状态
				HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
				warnDto.setWarnNo(warnNo);
				warnDto.setStatus(actCurrent);
				hcpmsWarnService.updateHcpmsWarn(warnDto);

				map.put("result", true);
				map.put("msg", "操作成功");
			} else {
				map.put("result", false);
				map.put("msg", "操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// }

		return map;
	}

	@RequestMapping("warnMonitor.do")
	@ResponseBody
	public List<WarnMonitorDTO> warnMonitor() throws BusinessServiceException, BusinessException {
		return warnResultService.findWarnMonitor();
	}

	@RequestMapping("warnMonitorRule.do")
	@ResponseBody
	public List<WarnMonitorDTO> warnMonitorRule() throws BusinessServiceException, BusinessException {
		return warnResultService.findWarnMonitorRule();
	}

	@RequestMapping("warnResultDetail.do")
	@ResponseBody
	public Map<String, Object> warnResultDetail(@RequestParam(value = "warnNo", defaultValue = "") String warnNo,
			HttpServletRequest request) throws BusinessServiceException, BusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("warnResults", warnResultService.warningResultQuery(warnNo));
		map.put("warnResultDetails", warnResultDetailService.warningResultQueryDetail(warnNo));
		return map;
	}
}
